<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <!--    搜索区    -->
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
          <!--部门数据-->
          <el-form-item label="机构单位" prop="deptId">
            <treeselect v-model="queryParams.deptId" :options="deptOptions" placeholder="请选择归属单位"
                        style="width: 200px;"/>
          </el-form-item>
          <el-form-item label="学年" prop="semester">
            <el-date-picker
              v-model="queryParams.year"
              type="year"
              placeholder="选择学年">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="学期" prop="semester">
            <el-select v-model="queryParams.semester" placeholder="请选择" clearable style="width: 240px">
              <el-option
                v-for="item in semester"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="缴费项目" prop="actName">
            <el-input
              v-model="queryParams.actName"
              placeholder="请输入缴费项目"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="学生" prop="studentName">
            <el-input
              v-model="queryParams.studentName"
              placeholder="请输入学生"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <!--    功能按钮    -->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['campus:act_stu:add']"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['campus:act_stu:edit']"
            >核销
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-view"
              size="mini"
              :disabled="single"
              @click="handleView"
              v-hasPermi="['campus:act_stu:edit']"
            >查看
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['campus:act_stu:remove']"
            >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['campus:act_stu:export']"
            >导出
            </el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <!-- 列表 -->
        <el-table v-loading="loading" :data="act_stuList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="业务流水" align="center" prop="payId"/>
          <el-table-column label="缴费项目" align="center" key="actName" prop="activity.actName"/>
          <el-table-column label="学生姓名" align="center" key="studentName" prop="students.studentName"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['campus:act_stu:edit']"
              >核销
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleView(scope.row)"
                v-hasPermi="['campus:act_stu:edit']"
              >查看
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['campus:act_stu:remove']"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页组件 -->
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

      </el-col>
    </el-row>


    <!-- 添加或修改缴费名单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-row>
        <el-descriptions title="缴费明细" :column="1" size="medium">
          <el-descriptions-item label="缴费项目">{{ activity.actName }}</el-descriptions-item>
          <el-descriptions-item label="缴费金额">{{ activity.totalPrice + ' 元' }}</el-descriptions-item>
          <el-descriptions-item label="缴费说明">{{ activity.remark }}</el-descriptions-item>
        </el-descriptions>
      </el-row>
      <el-row v-if="activity.remarkImg">
        <image-preview :src="activity.remarkImg" :width="50" :height="50"/>
      </el-row>
      <el-row>
        <div v-if="order!==null&&edit===false">
          <el-descriptions title="关联订单" :column="1" size="medium">
            <el-descriptions-item label="订单号">{{ order.id }}</el-descriptions-item>
            <el-descriptions-item label="订单名称">{{ order.orderName }}</el-descriptions-item>
            <el-descriptions-item label="订单详情">{{ order.orderDescrip }}</el-descriptions-item>
            <el-descriptions-item label="订单状态">
              <dict-tag :options="dict.type.trade_state" :value="order.status"/>
            </el-descriptions-item>
          </el-descriptions>
        </div>
      </el-row>
      <el-row>
        <div v-if="edit===true">
          <el-descriptions title="核销单号" :column="1" size="medium">
          </el-descriptions>
          <el-form ref="student" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="核销单号" prop="manualOrderId">
              <el-input v-model="form.manualOrderId" placeholder="核销单号"/>
            </el-form-item>
            <el-form-item label="核销金额" prop="manualTotalPrice">
              <el-input v-model="form.manualTotalPrice" placeholder="请输入核销金额"/>
            </el-form-item>
            <el-form-item label="操作人员" prop="manualUsername">
              <el-input v-model="form.manualUsername" placeholder="请输入操作人员姓名"/>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </div>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {listAct_stu, getAct_stu, delAct_stu, addAct_stu, updateAct_stu} from "@/api/campus/act_stu";
import {deptSchoolTreeSelect} from "@/api/campus/Org";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {addOrders} from "@/api/order/orders";

export default {
  name: "Act_stu",
  dicts: ['sys_normal_disable', 'trade_state'],
  components: {Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 缴费名单表格数据
      act_stuList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      edit: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        actId: null,
        actName: null,
        studentId: null,
        studentName: null,
        semester: null,
      },
      // 表单参数
      form: {},
      activity: {},
      student: {},
      order: {},
      payRecordList: [],
      // 表单校验
      rules: {},
      //学期
      semester: [{
        value: 'C',
        label: '春季'
      }, {
        value: 'Q',
        label: '秋季'
      }],
      /**==================================== 关于机构的数据 =====================================================*/
      // 部门树选项
      deptOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 部门名称
      deptName: undefined,
      /**==================================== 关于机构的数据 =====================================================*/
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询缴费名单列表 */
    getList() {
      this.loading = true;
      listAct_stu(this.queryParams).then(response => {
        this.act_stuList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.edit = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        payId: null,
        actId: null,
        studentId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.payId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加缴费名单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const payId = row.payId || this.ids
      getAct_stu(payId).then(response => {
        this.form = response.data;
        this.activity = response.data.activity;
        this.student = response.data.students;
        this.order = response.data.orders
        this.payRecordList = response.data.payRecordList
        this.open = true;
        this.edit = true;
        this.title = "修改缴费名单";
      });
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const payId = row.payId || this.ids
      getAct_stu(payId).then(response => {
        this.form = response.data;
        this.activity = response.data.activity;
        this.student = response.data.students;
        this.order = response.data.orders
        this.payRecordList = response.data.payRecordList
        this.open = true;
        this.title = "查询缴费名单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      const form = {
        totalPrice: this.activity.totalPrice,
        orderType: "60201",
        orderName: this.activity.actName,
        orderDescription: this.activity.remark,
        orderDetail: JSON.toString([this.activity]),
        outBusinessId: this.payId,
        manualOrderId:this.form.manualOrderId,
        manualTotalPrice:this.form.manualTotalPrice,
        manualUsername:this.form.manualUsername,

      }
      addOrders(form).then(response => {
        this.$modal.msgSuccess("下单");
        this.qrcode = response.data.qrcode;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const payIds = row.payId || this.ids;
      this.$modal.confirm('是否确认删除缴费名单编号为"' + payIds + '"的数据项？').then(function () {
        return delAct_stu(payIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('campus/act_stu/export', {
        ...this.queryParams
      }, `act_stu_${new Date().getTime()}.xlsx`)
    },
    /** ===============================================部门节点================================================ */
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptSchoolTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
    },
  }
};
</script>
